<template>
  <div class="login-bg-div">
    <div class="login-div">
      <p class="title-p">登录页面</p>
      <div class="form-div">
        <el-form :rules="rules"
                 :model="loginUser"
                 label-width="auto"
                 ref="ruleFormRef"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginUser.username"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginUser.password"/>
          </el-form-item>
          <el-form-item class="btn-item">
            <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
            <el-button type="warning" @click="resetForm(ruleFormRef)">清空</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

  <el-dialog
      v-model="dialogVisible"
      title="验证码"
      width="400"
  >
    <div style="padding-left: 30px">
      <slide-verify ref="slideBlock" @success="onSuccess" @again="onAgain" @fulfilled="onFulfilled" @fail="onFail"
                    @refresh="onRefresh" :slider-text="text" :imgs="imgs" :accuracy="accuracy"></slide-verify>
      <div>{{ msg }}</div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import api from "@/axios.js";
import {reactive, ref} from "vue";
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
import type {FormInstance, FormRules} from "element-plus";

interface LoginUser {
  username?: String,
  password?: String,
}

const ruleFormRef = ref<FormInstance>();
const dialogVisible = ref(false);
const loginUser = ref<LoginUser>({});

const rules = reactive<FormRules<LoginUser>>({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      dialogVisible.value = true;
    } else {
      ElMessage("请按照规则填写")
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

function login() {
  api({
    url: "/login",
    method: "post",
    params: loginUser.value
  }).then(resp => { // 回调函数
    ElMessage(resp.data.message);
    if (resp.data.code == 1)
      router.push("/index"); // 跳转到系统主页

  })
}

// 验证码
import img1 from '../assets/img1.jpg';
import img2 from '../assets/img2.jpg';
import img3 from '../assets/img3.jpg';
import img4 from '../assets/img4.jpg';

const slideBlock = ref();
const msg = ref('');
const text = ref('向右滑动->');
const imgs = ref([
  img1,
  img2,
  img3,
  img4
]);
const accuracy = ref(2); // 精确度小，可允许的误差范围小；为1时，则表示滑块要与凹槽完全重叠，才能验证成功。默认值为5


const onSuccess = (times) => {
  msg.value = `login success, 耗时${(times / 1000).toFixed(1)}s`;
  dialogVisible.value = false;
  login();
};

const onFail = () => {
  msg.value = ''
};
const onRefresh = () => {
  console.log('点击了刷新小图标');
  msg.value = ''
};
const onFulfilled = () => {
  console.log('刷新成功啦！');
};
const onAgain = () => {
  console.log('检测到非人为操作的哦！');
  msg.value = 'try again';
  // 刷新
  handleClick();
};
const handleClick = () => {
  slideBlock.value.reset();
  msg.value = ''
}
</script>

<style scoped>
.login-bg-div {
  width: 100%;
  height: 100%;
  background-image: url("../assets/login-bg.jpg");
  background-size: cover;
}

.login-div {
  padding-top: 20px;
  padding-left: 40px;
  border: 3px #ddcece solid;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 1);
  position: fixed;
  width: 340px;
  height: 370px;
  margin: 140px 35%;
}

.title-p {
  margin-left: 65px;
  font-family: cursive;
  font-size: 40px;
  font-weight: bold;
}

.form-div {
  padding-right: 50px;
}

.btn-item{
  padding-top: 40px;
  padding-left: 20%;
}

</style>